package com.watermark.ui;

import android.graphics.Bitmap;
import android.os.Environment;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.watermark.core.utils.UIKit;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class PathUtil {
    public static String ROOT_DIR = "waterMark/";
    public static String VIDEO_DRI = "video/";
    public static String PICTURE_DRI = "picture/";
    public static String COVER = "_cover.jpeg";// 封面
    public static String MP4 = ".mp4";
    public static String JPEG = ".jpeg";

    /**
     * 获取文件存储根路径：
     * 外部存储可用，返回外部存储路径:/storage/emulated/0/Android/data/包名/files
     * 外部存储不可用，则返回内部存储路径：data/data/包名/files
     */
    public static String getFilesPath() {
        String filePath;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            //外部存储可用:/storage/emulated/0/Android/data/包名/files
            filePath = UIKit.getContext().getExternalFilesDir(null).getPath();
        } else {
            //外部存储不可用，内部存储路径：data/data/com.learn.test/files
            filePath = UIKit.getContext().getFilesDir().getPath();
        }
        return filePath;
    }

    /**
     * 获取文件存储根路径：
     * 外部存储可用，返回外部存储路径:/storage/emulated/0/Android/data/包名/cache
     * 外部存储不可用，则返回内部存储路径：data/data/包名/cache
     */
    public static String getCachePath() {
        String cachePath;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            //外部存储可用：/storage/emulated/0/Android/data/包名/cache
            cachePath = UIKit.getContext().getExternalCacheDir().getPath();
        } else {
            //外部存储不可用：/data/data/com.learn.test/cache
            cachePath = UIKit.getContext().getCacheDir().getPath();
        }
        return cachePath;
    }

    public static String getPath(@NonNull String dir, String fileName) {
        String root = getFilesPath();
        if (TextUtils.isEmpty(dir)) {
            return root + File.separator + fileName;
        }
        if (!dir.startsWith("/")) {
            dir = File.separator + dir;
        }
        if (!dir.endsWith("/")) {
            dir = dir + File.separator;
        }
        String p = root + dir;
        File f = new File(p);
        if (!f.exists() && !f.mkdirs()) {
            return root + File.separator + fileName;
        }
        return p + fileName;
    }

    public static String getVideoPath() {
        return getPath(VIDEO_DRI, System.currentTimeMillis() + MP4);
    }

    public static String getVideoPath(String fileName) {
        return getPath(VIDEO_DRI, fileName);
    }

    public static String getPicturePath() {
        return getPath(PICTURE_DRI, System.currentTimeMillis() + JPEG);
    }

    public static String getPicturePath(String fileName) {
        return getPath(PICTURE_DRI, fileName);
    }

    public static String getCover(String videoPath) {
        if (!TextUtils.isEmpty(videoPath) && videoPath.endsWith(MP4)) {
            return videoPath.replace(MP4, COVER);
        }
        return "";
    }

    public static String saveBmp2File(Bitmap bmp) {
        if (null == bmp) return "";
        String path = getPicturePath();
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(path);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            return path;
        } catch (Exception e) {
            e.getStackTrace();
        } finally {
            bmp.recycle();
            try {
                if (outStream != null) {
                    outStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    public static boolean saveBitmap2File(Bitmap bmp, String path) {
        if (null == bmp) return false;
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(path);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            return true;
        } catch (Exception e) {
            e.getStackTrace();
        } finally {
            bmp.recycle();
            try {
                if (outStream != null) {
                    outStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
